package org.example.springbootblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.example.springbootblog.common.pojo.dataobject.BlogInfo;
import org.example.springbootblog.common.pojo.request.AddBlogParam;
import org.example.springbootblog.common.pojo.request.UpBlogParam;
import org.example.springbootblog.common.pojo.response.BlogInfoResponse;
import org.example.springbootblog.common.pojo.utils.BeanConver;
import org.example.springbootblog.mapper.BlogInfoMapper;
import org.example.springbootblog.service.BlogService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogInfoMapper blogInfoMapper;
    //获取博客列表
    @Override
    public List<BlogInfoResponse> getList() {
        //查询数据库
        List<BlogInfo> blogInfos = blogInfoMapper.selectList(new LambdaQueryWrapper<BlogInfo>()
                .eq(BlogInfo::getDeleteFlag,0).orderByDesc(BlogInfo::getId));
        //转换
        List<BlogInfoResponse> blogInfoResponses = blogInfos.stream().map(blogInfo ->
                        BeanConver.trans(blogInfo)).collect(Collectors.toList());

        return blogInfoResponses;
    }

    @Override
    public BlogInfoResponse getBlogDetail(Integer blogId) {
        BlogInfo blogInfo = selectBlogById(blogId);
        return BeanConver.trans(blogInfo);
    }

    @Override
    public Boolean addBlog(AddBlogParam param) {
        //对象转换    AddBlogParam -> BlogInfo
        BlogInfo blogInfo = BeanConver.trans(param);
        //插入博客
        try {
            int result = blogInfoMapper.insert(blogInfo);
            if (result == 1) {
                return true;
            }
        }catch (Exception e) {
            log.error("博客插入失败,e",e);
        }
        return false;
    }

    @Override
    public Boolean updateBlog(UpBlogParam upBlogParam) {
        //对象转换   UpBlogParam -> BlogInfo
        BlogInfo blogInfo = BeanConver.trans(upBlogParam);
        //更新博客
        return update(blogInfo);
    }

    @Override
    public Boolean deleteBlog(Integer blogId) {
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);
       return update(blogInfo);
    }


    //从数据库中查询博客详情
    public BlogInfo selectBlogById(Integer blogId) {
        return blogInfoMapper.selectOne(new LambdaQueryWrapper<BlogInfo>().eq(BlogInfo::getId,blogId).eq(BlogInfo::getDeleteFlag,0));
    }

    //把公共代码提出来  更新博客
    public Boolean update(BlogInfo blogInfo) {
        try {
            Integer result = blogInfoMapper.updateById(blogInfo);
            if (result==1) {
                return true;
            }
        }catch (Exception e) {
            log.error("更新博客失败, e:",e);
        }
        return false;
    }
}
